package com.tanhua.manage.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.manage.service.DashboardService;
import com.tanhua.manage.vo.DistributionVo;
import com.tanhua.manage.vo.SummaryVo;
import com.tanhua.manage.vo.UsersDistributionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Ling
 * @Title: null.java
 * @Package com.tanhua.manage.controller
 * @Description:
 * @date 2021/9/11 16:28
 */
@RestController
@RequestMapping("dashboard")
public class DashboardController {

    @Autowired
    DashboardService dashboardService;

    @GetMapping("distribution")
    public ResponseEntity<DistributionVo> getDistribution(@RequestParam Long sd, @RequestParam Long ed) {
        try {
            if (ObjectUtil.isAllNotEmpty(sd, ed)) {
                DistributionVo distributionVo = dashboardService.getDistribution(sd, ed);
                if (!ObjectUtil.isEmpty(distributionVo)) {
                    return ResponseEntity.ok(distributionVo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("summary")
    public ResponseEntity<SummaryVo> getSummary() {
        try {
            SummaryVo summaryVo = dashboardService.getSummary();
            if (!ObjectUtil.isEmpty(summaryVo)) {
                return ResponseEntity.ok(summaryVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("users")
    public ResponseEntity<UsersDistributionVo> getUsers(@RequestParam Long sd, @RequestParam Long ed, @RequestParam String type) {
        try {
            if (ObjectUtil.isAllNotEmpty(sd, ed, type)) {
                UsersDistributionVo users = dashboardService.getUsers(sd, ed, type);
                if (!ObjectUtil.isEmpty(users)) {
                    return ResponseEntity.ok(users);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
